﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Us.Mil.Osd.Acq.Damir.Orca;
using BusinessObjects.WCF.DAL;
using Microsoft.CSharp.RuntimeBinder;

namespace TestProject3
{
	//[TestClass]
	public class ConvertExtensionTest
	{

		static void Main()
		{
			var test = new ConvertExtensionTest();
			//test.NewSecureContractTypeItem();
			test.ConvertContractDataPoint();
		}

		//[TestMethod]
		public void ConvertContractDataPoint()
		{
			var contractdata = GetFromDAL<Us.Mil.Osd.Acq.Damir.Tamarin.ContractDataPoint>().Take(10).ToArray();
			List<BusinessObjects.SL.Tamarin.ContractDataPoint> convertedList = new List<BusinessObjects.SL.Tamarin.ContractDataPoint>();
			foreach (var c1 in contractdata)
			{
				var c2 = BusinessObjects.SL.ConvertExtension.Convert(c1);
				convertedList.Add(c2);
			}
			
		}

		//[TestMethod()]
		public void NewNullableType()
		{
			dynamic src2, dest2;
			src2 = new System.Nullable<Us.Mil.Osd.Acq.Damir.Orca.SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>>(new SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>(Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum.CR));
			if (src2 is System.Nullable<Us.Mil.Osd.Acq.Damir.Orca.SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>>)
			{
				dest2 = new System.Nullable<BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>>();
				try
				{
					dest2.HasValue = src2.HasValue;//RuntimeBinderException
				}
				catch (RuntimeBinderException rbex)
				{
					dest2 = default(System.Nullable<BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>>);
				}
			}                        
		}

		//[TestMethod]
		public void NewSecureContractTypeItem()
		{
			dynamic dest1, dest2, src2, dest3, src3, dest4, src4;
			var src1 = new Us.Mil.Osd.Acq.Damir.Tamarin.SecureContractTypeItem();
			src1.Value = new SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>(Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum.CR);
			dest1 = new BusinessObjects.SL.Tamarin.SecureContractTypeItem();
			src2 = src1.Value;
			if (src2 is System.Nullable<Us.Mil.Osd.Acq.Damir.Orca.SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>>)
			{
				Type dest2Type = typeof(System.Nullable<BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>>);
				if (dest2Type.IsValueType && dest2Type.Name == "Nullable`1")
				{
					var def = default(BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>);
					dest2 = new System.Nullable<BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>>(def);
				}
				else
					dest2 = new System.Nullable<BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>>();					
				dest2.HasValue = src2.HasValue;
				src3 = src2.Value;
				if (src3 is Us.Mil.Osd.Acq.Damir.Orca.SecureEnum<Us.Mil.Osd.Acq.Damir.Tamarin.ContractTypeEnum>)
				{
					dest3 = new BusinessObjects.SL.Orca.SecureEnum<BusinessObjects.SL.Tamarin.ContractTypeEnum>();
					src4 = src3.SecurityDescriptor;
					if (src4 is Us.Mil.Osd.Acq.Damir.Orca.SecurityDescriptor)
					{
						dest4 = new BusinessObjects.SL.Orca.SecurityDescriptor();
						dest3.SecurityDescriptor = dest4;
					}
					dest2.Value = dest3;
				}
				dest1.Value = dest2;
			}
		}


		static IEnumerable<T> GetFromDAL<T>() where T :SequencedObject
		{			
			DataAccess da = new SqlDataAccess(SqlDataAccess.MSSQLSERVER_DAMIRDEV);
			return da.Get<T>();
		}
	}
}
